<?php
namespace Framework;

class OutputGenerator {
	public function addPageComponent(PageComponent $component) {
		$this->components[] = $component;
	}
	
	public function render() {
		$jsIncludes = array();
		$stylesheets = array();
		$renderBuffer = '';
		$scriptBuffer = '';
		
		foreach($this->components as $component) {
			foreach($component->getJSIncludes() as $path) {
				if(!isset($jsIncludes[$path])) {
					$jsIncludes[$path] = true;
				}
			}
			
			foreach($component->getStylesheets() as $path) {
				if(!isset($stylesheets[$path])) {
					$stylesheets[$path] = true;
				}
			}
			
			$renderBuffer .= $component->render();
			$scriptBuffer .= $component->getScript();
		}
		// output header
		echo '<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Welcome to CodeIgniter</title>';
	
		// output CSS
		foreach($stylesheets as $path => $dummy) {
			echo '<link rel="stylesheet" href="'.$path.'"></script>';
		}
		// output JS
		foreach($jsIncludes as $path => $dummy) {
			echo '<script type="text/javascript" language="javascript" src="'.$path.'"></script>';
		}
		// output body
		echo '</head><body>';
		// output components
		echo $renderBuffer;
		// output footer
		// noop for now
		// output scripts
		echo $scriptBuffer;
		// output close tags
		echo '</body></html>';
	}
}